{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "42b78997-14d2-4b90-bfe1-bdb6f1ad1e1e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请求成功!\n",
      "页面内容已保存到当前文件夹: 100095516761.html\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "\n",
    "url = \"https://item.jd.com/100095516761.html\"\n",
    "\n",
    "headers = {\n",
    "    \"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36\"\n",
    "}\n",
    "\n",
    "response = requests.get(url, headers=headers)\n",
    "\n",
    "if response.status_code == 200:\n",
    "    print(\"请求成功!\")\n",
    "    \n",
    "    # 从URL中提取文件名\n",
    "    filename = url.split(\"/\")[-1]\n",
    "    \n",
    "    # 确保文件有.html后缀\n",
    "    if not filename.endswith('.html'):\n",
    "        filename += '.html'\n",
    "    \n",
    "    # 保存响应内容到文件\n",
    "    with open(filename, 'w', encoding='utf-8') as f:\n",
    "        f.write(response.text)\n",
    "    \n",
    "    print(f\"页面内容已保存到当前文件夹: {filename}\")\n",
    "else:\n",
    "    print(\"请求失败,状态码:\", response.status_code)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "67ce2a88-c107-4b45-a7fb-c5f81efb3534",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
